﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyNhaSach
{
    public partial class QuanLySach : Form
    {
        public QuanLySach()
        {
            InitializeComponent();
        }

        private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.AliceBlue, e.Bounds);
            bool select = e.Index == tabControl1.SelectedIndex;
            if (select)
            {
                e.Graphics.FillRectangle(Brushes.Aqua, e.Bounds);
            }
            string name = tabControl1.TabPages[e.Index].Text;
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;
            e.Graphics.DrawString(name, this.Font, Brushes.Black, e.Bounds, new StringFormat(stringFormat));
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            phieu_nhap.BeginEdit(true);
        }

        private void QuanLySach_Load(object sender, EventArgs e)
        {
            BusinessLogicLayer.QuanLySachBLL qls = new BusinessLogicLayer.QuanLySachBLL();
            DataTable data = qls.LayDanhSach();
            list_danh_sach_sach.DataSource = data;
            list_danh_sach_sach.DisplayMember = "TenSach";
            txt_ma_3.DataBindings.Add("text", data, "MaSach");
            txt_ten_3.DataBindings.Add("text", data, "TenSach");
            txt_tacgia_3.DataBindings.Add("text", data, "TacGia");
            txt_theloai_3.DataBindings.Add("text", data, "TheLoai");
            txt_soluong_3.DataBindings.Add("text", data, "SoLuong");


//             txt_tim_kiem.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
//             txt_tim_kiem.AutoCompleteSource = AutoCompleteSource.CustomSource;
//             txt_tim_kiem.AutoCompleteCustomSource = qls.TaoAutocompleteTen();
            txt_maSach_2.Text = qls.TaoMaSach();            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            txt_author_2.BackColor = Color.White;
            txt_type_2.BackColor = Color.White;
            txt_tenSach_2.BackColor = Color.White;
            bool error = false;
            if (txt_author_2.Text == "")
            {
                txt_author_2.BackColor = Color.Red;
                error = true;
            }
            if (txt_tenSach_2.Text == "")
            {
                txt_tenSach_2.BackColor = Color.Red;
                error = true;
            }
            if (txt_type_2.Text == "")
            {
                txt_type_2.BackColor = Color.Red;
                error = true;
            }
            if (error) return;
            txt_author_2.BackColor = Color.White;
            txt_type_2.BackColor = Color.White;
            txt_tenSach_2.BackColor = Color.White;
            BusinessLogicLayer.QuanLySachBLL qls = new BusinessLogicLayer.QuanLySachBLL();
            qls.ThemSach(txt_maSach_2.Text, txt_tenSach_2.Text, txt_type_2.Text, txt_author_2.Text);
            txt_author_2.Text = "";
            txt_maSach_2.Text = qls.TaoMaSach();
            txt_tenSach_2.Text = "";
            txt_type_2.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bool error = false;
            txt_ten_3.BackColor = Color.White;
            txt_soluong_3.BackColor = Color.White;
            txt_tacgia_3.BackColor = Color.White;
            txt_theloai_3.BackColor = Color.White;
            if (txt_ten_3.Text == "")
            {
                txt_ten_3.BackColor = Color.Red;
                error = true;
            }

            if (txt_theloai_3.Text == "")
            {
                txt_theloai_3.BackColor = Color.Red;
                error = true;
            }

            if (txt_tacgia_3.Text == "")
            {
                txt_tacgia_3.BackColor = Color.Red;
                error = true;
            }

            if (txt_soluong_3.Text == "")
            {
                txt_soluong_3.BackColor = Color.Red;
                error = true;
            }
            if (error) return;
            txt_ten_3.BackColor = Color.White;
            txt_soluong_3.BackColor = Color.White;
            txt_tacgia_3.BackColor = Color.White;
            txt_theloai_3.BackColor = Color.White;
            BusinessLogicLayer.QuanLySachBLL qls = new BusinessLogicLayer.QuanLySachBLL();
            qls.CapNhatSach(txt_ma_3.Text, txt_ten_3.Text, txt_theloai_3.Text, txt_tacgia_3.Text, txt_soluong_3.Text);
        }

        private void phieu_nhap_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (phieu_nhap.CurrentCell == null) return;
            if (phieu_nhap.CurrentCell.ColumnIndex == 0)
            {
                BusinessLogicLayer.QuanLySachBLL qls = new BusinessLogicLayer.QuanLySachBLL();
                TextBox txt = e.Control as TextBox;
                txt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txt.AutoCompleteCustomSource = qls.TaoAutocompleteMa();
            }
            
        }

        private void phieu_nhap_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (phieu_nhap.CurrentCell.Value == null) return;
            if (phieu_nhap.CurrentCell.ColumnIndex == 0)
            {
                BusinessLogicLayer.QuanLySachBLL qls = new BusinessLogicLayer.QuanLySachBLL();
                DataTable data = qls.XemThongTinSach(phieu_nhap.CurrentCell.Value.ToString());
                int r = phieu_nhap.CurrentCell.RowIndex;
                foreach (DataGridViewCell c in phieu_nhap.Rows[r].Cells)
                {
                    if (c.ColumnIndex == 0) continue;
                    if (c.ColumnIndex == 5)
                    {
                        c.Value = "0";
                        continue;
                    }
                    c.Value = data.Rows[0][c.ColumnIndex];
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BusinessLogicLayer.PhieuNhapBLL pn = new BusinessLogicLayer.PhieuNhapBLL();
            bool error = false;
             foreach(DataGridViewRow r in phieu_nhap.Rows)
             {
                 if (r.Index == phieu_nhap.Rows.Count - 1)
                 {
                     continue;
                 }
                 
                 BusinessLogicLayer.DIEU_KIEN_NHAP dk = pn.KiemTraDieuKienNhap(r.Cells["MaSach"].Value.ToString(), r.Cells["SoLuong"].Value.ToString());
                 switch (dk)
                 {
                     case BusinessLogicLayer.DIEU_KIEN_NHAP.KHONG_THOA_TON:
                         r.ErrorText = "Đầu sách còn tồn nhiều hơn quy định";
                         error = true;
                         break;
                     case BusinessLogicLayer.DIEU_KIEN_NHAP.KHONG_THOA_NHAP_TOI_THIEU:
                         r.ErrorText = "Lượng nhập tối thiểu chưa đáp ứng";
                         error = true;
                         break;
                 }                 
             }
             if (error)
             {
                 MessageBox.Show("Đã xảy ra lỗi!", "Quản lý nhập sách");
             }
             else
             {
                 BusinessLogicLayer.QuanLySachBLL qls = new BusinessLogicLayer.QuanLySachBLL();
                 string ma_pn = pn.TaoMaPhieuNhap();
                 pn.ThemPhieuNhap(ma_pn, DateTime.Now.Date.ToShortDateString());
                 foreach (DataGridViewRow r in phieu_nhap.Rows)
                 {
                     if (r.Index == phieu_nhap.Rows.Count - 1) continue;
                     float gia = int.Parse(r.Cells["GiaNhap"].Value.ToString()) * 1.05f;
                     qls.CapNhatGiaSach(r.Cells["MaSach"].  Value.ToString(), gia.ToString());
                     pn.ThemChiTiet(ma_pn, r.Cells["MaSach"].Value.ToString(), r.Cells["SoLuong"].Value.ToString(), r.Cells["GiaNhap"].Value.ToString());
                 }
                 phieu_nhap.Rows.Clear();
                 MessageBox.Show("Nhập thành công!");

             }
        }

        private void txt_tim_kiem_Click(object sender, EventArgs e)
        {
            txt_tim_kiem.Text = "";
        }

        private void txt_tim_kiem_TextChanged(object sender, EventArgs e)
        {
               BusinessLogicLayer.QuanLySachBLL qls = new BusinessLogicLayer.QuanLySachBLL();

               DataTable data = qls.TimKiemSach(txt_tim_kiem.Text);
               list_danh_sach_sach.DataSource = data;
               list_danh_sach_sach.DataSource = data;
               list_danh_sach_sach.DisplayMember = "TenSach";
               txt_ma_3.DataBindings.Clear();
               txt_ten_3.DataBindings.Clear();
               txt_tacgia_3.DataBindings.Clear();
               txt_theloai_3.DataBindings.Clear();
               txt_soluong_3.DataBindings.Clear();


               txt_ma_3.DataBindings.Add("text", data, "MaSach");
               txt_ten_3.DataBindings.Add("text", data, "TenSach");
               txt_tacgia_3.DataBindings.Add("text", data, "TacGia");
               txt_theloai_3.DataBindings.Add("text", data, "TheLoai");
               txt_soluong_3.DataBindings.Add("text", data, "SoLuong");
        }
    }
}
